package com.iflytek.exam.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iflytek.common.constant.HttpStatus;
import com.iflytek.common.core.page.PageDomain;
import com.iflytek.common.core.page.TableSupport;
import com.iflytek.exam.dto.TbAllDTO;
import com.iflytek.exam.service.IChoiceService;
import com.iflytek.exam.service.IJudgeService;
import com.iflytek.exam.service.IShortAnswerService;
import com.iflytek.exam.utils.PageUtil;
import com.iflytek.exam.vo.CategoryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iflytek.common.annotation.Log;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.exam.domain.TbAll;
import com.iflytek.exam.service.ITbAllService;
import com.iflytek.common.utils.poi.ExcelUtil;
import com.iflytek.common.core.page.TableDataInfo;

/**
 * 试题管理Controller
 *
 * @author Dogdog
 * @date 2024-07-06
 */
@RestController
@RequestMapping("/exam/all")
public class TbAllController extends BaseController
{
    @Autowired
    private ITbAllService tbAllService;
    @Autowired
    private IChoiceService choiceService; //选择
    @Autowired
    private IJudgeService judgeService; //判断
    @Autowired
    private IShortAnswerService shortAnswerService; //简答

    /**
     * 查询试题管理列表
     */
    //@PreAuthorize("@ss.hasPermi('exam:all:list')")
    //@GetMapping("/list")
    //public TableDataInfo list(TbAll tbAll)
    //{
    //    //startPage();
    //    //List<TbAllDTO> list = tbAllService.selectTbAllDTOList(tbAll);
    //    //return getDataTable(list);
    //    //startPage();
    //    PageDomain pageDomain = TableSupport.buildPageRequest();
    //    Integer pageNum = pageDomain.getPageNum();
    //    Integer pageSize = pageDomain.getPageSize();
    //    List<TbAllDTO> list = tbAllService.selectTbAllDTOList(tbAll);
    //    //处理上面查询的list集合
    //    int num = list.size();
    //    list = list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
    //    TableDataInfo rspData = new TableDataInfo();
    //    rspData.setCode(HttpStatus.SUCCESS);
    //    rspData.setRows(list);
    //    rspData.setTotal(num);
    //    return rspData;
    //    //return getDataTable(list);
    //}

    @GetMapping("/list/category")
    public CategoryVo categoryList() {
        return tbAllService.selectCategoryList();
    }
    @GetMapping("/list")
    public TableDataInfo list(TbAll tbAll) {
        //获取前端的分页参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        //查询出的集合
        List<TbAllDTO> list = tbAllService.selectTbAllDTOList(tbAll);

        //使用自定义分页查询并返回数据
        return PageUtil.getPageInfo(list, pageNum, pageSize);
    }
    //@GetMapping("/list")
    //public TableDataInfo list(TbAll tbAll) {
    //    startPage();
    //    PageDomain pageDomain = TableSupport.buildPageRequest();
    //    Integer pageNum = pageDomain.getPageNum();
    //    Integer pageSize = pageDomain.getPageSize();
    //
    //    IPage<TbAllDTO> page = tbAllService.selectTbAllDTOListByPage(tbAll, pageNum, pageSize);
    //    TableDataInfo rspData = new TableDataInfo();
    //    rspData.setCode(HttpStatus.SUCCESS);
    //    rspData.setRows(page.getRecords());
    //    rspData.setTotal(page.getTotal());
    //    return rspData;
    //}
    /**
     * 导出试题管理列表
     */
    @PreAuthorize("@ss.hasPermi('exam:all:export')")
    @Log(title = "试题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAll tbAll)
    {
        List<TbAll> list = tbAllService.selectTbAllList(tbAll);
        ExcelUtil<TbAll> util = new ExcelUtil<TbAll>(TbAll.class);
        util.exportExcel(response, list, "试题管理数据");
    }

    /**
     * 获取试题管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:all:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbAllService.selectTbAllById(id));
    }

    /**
     * 新增试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:all:add')")
    @Log(title = "试题管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAll tbAll)
    {
        return toAjax(tbAllService.insertTbAll(tbAll));
    }

    /**
     * 修改试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:all:edit')")
    @Log(title = "试题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAll tbAll)
    {
        return toAjax(tbAllService.updateTbAll(tbAll));
    }

    /**
     * 删除试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:all:remove')")
    @Log(title = "试题管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        Long[] longs1 = new Long[0]; // 选择题
        Long[] longs2 = new Long[0]; // 判断题
        Long[] longs3 = new Long[0]; // 填空题
        for (Long id : ids) {
            TbAll tbAll = tbAllService.selectTbAllById(id);
            Integer type = tbAll.getType(); // 获取到他的试题类型
            Long id1 = tbAll.getAllId();
            switch (type) {
                case 1:
                    //单选
                    longs1 = addToArray(longs1, id1);
                    break;
                case 2:
                    //多选
                    longs1 = addToArray(longs1, id1);
                    break;
                case 3:
                    longs2 = addToArray(longs2, id1);
                    break;
                case 4:
                    longs3 = addToArray(longs3, id1);
                    break;
                default:
                    // 可以在这里处理未知类型的 ID，或者什么都不做
                    break;
            }
        }
        // 执行删除操作
        if (longs1.length > 0) {
            choiceService.deleteChoiceByIds(longs1);
        }
        if (longs2.length > 0) {
            judgeService.deleteJudgeByIds(longs2);
        }
        if (longs3.length > 0) {
            shortAnswerService.deleteShortAnswerByIds(longs3);
        }
        // 打印语句
        System.out.println(Arrays.toString(longs1));
        System.out.println(Arrays.toString(longs2));
        System.out.println(Arrays.toString(longs3));
        // 删除所有试题
        return toAjax(tbAllService.deleteTbAllByIds(ids));
    }
    // 辅助方法，用于将 ID 添加到数组中
    private Long[] addToArray(Long[] array, Long element) {
        //防止数组越界
        Long[] newArray = Arrays.copyOf(array, array.length + 1);
        newArray[array.length] = element;
        return newArray;
    }




    //获取一级分类
    @GetMapping("/getCategoryFirst")
    public TableDataInfo getCategoryFirst()
    {
        List<String> list = tbAllService.getCategoryFirst();
        return getDataTable(list);
    }

    @GetMapping("/getCategorySecond")
    public AjaxResult getCategorySecond()
    {
        List<String> list = tbAllService.getCategorySecond();
        return success(list);
    }

    @GetMapping("/getCategoryThrid")
    public AjaxResult getCategoryThrid()
    {
        List<String> list = tbAllService.getCategoryThrid();
        return success(list);
    }



}
